package com.example.demo.controller;

import com.example.demo.entity.address;
import com.example.demo.mapper.addressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
public class addressController {
    @Autowired
    addressMapper addressMapper;

    @GetMapping("/selectaddresss/{user}")
    public List<address> selectaddresss(@PathVariable("user") String user) {
        return addressMapper.selectAddress(user);
    }

    @GetMapping("/addAddress/{user}/{defaultaddress}/{province}/{city}/{area}/{detailed_address}/{phone}/{name}")
    public address addAddress(@PathVariable("user") String user, @PathVariable("defaultaddress") int defaultaddress, @PathVariable("province") String province,
                              @PathVariable("city") String city, @PathVariable("area") String area, @PathVariable("detailed_address") String detailed_address,
                              @PathVariable("phone") String phone, @PathVariable("name") String name) {
        address a = new address();
        a.setUser(user);
        a.setDefaultaddress(defaultaddress);
        a.setProvince(province);
        a.setCity(city);
        a.setArea(area);
        a.setPhone(phone);
        a.setName(name);
        a.setDetailed_address(detailed_address);
        addressMapper.addAddress(a);
        return a;
    }

    @GetMapping("deleteAddressbyid/{id}")
    public void deleteAddressbyid(@PathVariable("id") int id) {
        addressMapper.deleteAddressbyid(id);
    }

    @GetMapping("/updateaddress2/{id}/{defaultaddress}/{province}/{city}/{area}/{detailed_address}/{phone}/{name}")
    public address updateaddress2(@PathVariable("defaultaddress") int defaultaddress, @PathVariable("province") String province,
                               @PathVariable("city") String city, @PathVariable("area") String area, @PathVariable("detailed_address") String detailed_address,
                               @PathVariable("phone") String phone, @PathVariable("name") String name,@PathVariable("id") int id) {
        address a = new address();
        a.setDefaultaddress(defaultaddress);
        a.setProvince(province);
        a.setCity(city);
        a.setArea(area);
        a.setPhone(phone);
        a.setName(name);
        a.setDetailed_address(detailed_address);
        a.setId(id);
        addressMapper.updateAddress();
        addressMapper.updateaddress2(a);
        return a;
    }
}
